import exp.BmpStructureErrorException;
import utils.BasicJPanel;
import utils.VFlowLayout;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MainWindow extends JFrame implements ActionListener
{
    private       Path        path;
    private       BasicJPanel basicJPanel;
    private final JButton     selectJButton;

    private class Item
    {
        String      name;
        JButton     button;
        BasicJPanel jPanel;

        public Item(String className)
        {
            try
            {
                Class<?> aClass=Class.forName(className);
                jPanel=((BasicJPanel)aClass.newInstance());
                name  =jPanel.getButtonName();
                button=new JButton(name);
                button.addActionListener(MainWindow.this);
                button.setHorizontalAlignment(SwingConstants.LEFT);
            }catch(ClassNotFoundException|IllegalAccessException|InstantiationException e)
            {
                e.printStackTrace();
            }
        }

        public BasicJPanel getInstance()
        {
            return jPanel;
        }

        public JButton getButton()
        {
            return button;
        }
    }

    final Item[] items;

    MainWindow()
    {
        super("202000800593李俊彦-数字图像处理");
        setSize(1500,900);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());


        JPanel infoJPanel=new JPanel();
        infoJPanel.setBorder(new TitledBorder("MENU"));
        JScrollPane jScrollPane=new JScrollPane(infoJPanel);
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(jScrollPane,BorderLayout.WEST);
        infoJPanel.setLayout(new VFlowLayout());

        selectJButton=new JButton("选择文件");
        infoJPanel.add(selectJButton);
        selectJButton.addActionListener(this);
        items=new Item[]{
                new Item("exp.BmpJPanel"),
                new Item("exp.GrayJPanel"),
                new Item("exp.TranslationJPanel"),
                new Item("exp.MirrorJPanel"),
                new Item("exp.ShareJPanel"),
                new Item("exp.GrayHistogramJPanel"),
                new Item("exp.GrayTransformationJPanel"),
                new Item("exp.saltAndPepperNoiseJPanel"),
                new Item("exp.SobelJPanel"),
                new Item("exp.PrewittJPanel"),
                };
        for(Item item: items)
            infoJPanel.add(item.getButton());

        path=Paths.get("彩色lena图像256色.BMP");
        add(basicJPanel=items[items.length-1].getInstance(),BorderLayout.CENTER);
        update();

        setVisible(true);
    }

    public static void main(String[] args)
    {
        new MainWindow();

    }

    private void update()
    {
        try
        {
            basicJPanel.update(path);
            validate();
            repaint();
        }catch(IOException e)
        {
            JOptionPane.showMessageDialog(this,"IO错误","错误",JOptionPane.ERROR_MESSAGE);
        }catch(BmpStructureErrorException e)
        {
            JOptionPane.showMessageDialog(this,"BMP结构错误","错误",JOptionPane.ERROR_MESSAGE);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource().equals(selectJButton))
        {
            JFileChooser jFileChooser=new JFileChooser(path.toFile());
            jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            jFileChooser.showDialog(new JLabel(),"选择");
            path=jFileChooser.getSelectedFile().toPath();
            update();
        }
        else
        {
            for(Item item: items)
                if(e.getSource().equals(item.getButton()))
                {
                    remove(basicJPanel);
                    add(basicJPanel=item.getInstance(),BorderLayout.CENTER);
                    update();
                    break;
                }
        }
    }
}








